
<?php
/*
    database.php
    Part of the Wikked Paste project - version 1.0
    9/9/2011
    Adam Orama - TheHaxx0r1215@gmail.com
    
    MySQL database functions.
*/
?>

<?php
    function database_connect ()
    {
        require ( "../config/databaseconfig.php" );
        $sql_connection = @mysql_connect ( $host, $user, $password );
        if ( !$sql_connection ) {
            die ( "Could not connect to MySQL server!" );
        }
        if ( !mysql_select_db ( $dbname ) ) {
            if ( !mysql_query ( "CREATE DATABASE " . $dbname ) ) {
                die ( "Unable to create database: " . mysql_error () );
            }
            if ( !mysql_select_db ( $dbname ) ) {
                die ( "Database creation error: " . mysql_error () );
            }
        }
        $query  = "CREATE TABLE IF NOT EXISTS";
        $query .= " Entries ( ID TEXT, Date TIME, Language TEXT, Text TEXT, Title TEXT, Expiry TEXT, Privacy TEXT, MemberName TEXT )";
        if ( !mysql_query ( $query ) ) {
            die ( "Unable to create table: " . mysql_error () . "<br>" );
        }
    }

    function database_insert ( $id, $language, $text, $title, $expiry, $privacy )
    {
        $query  = "INSERT INTO Entries ( ID, Date, Language, Text, Title, Expiry, Privacy, MemberName )";
        $query .= " VALUES ( '$id', CURRENT_TIME(), '$language', '$text', '$title', '$expiry', '$privacy'" . "'"$_SESSION['SESS_FIRST_NAME']"'";
        if ( !mysql_query ( $query ) ) {
            die ( "Unable to perform insertion query: " . mysql_error () );
        }
    }

    function database_retrieve ( $id )
    {
        $entry = mysql_query ( "SELECT * FROM Entries WHERE ID = '$id'" );
        if ( !$entry ) {
            die ( "Query error: " . mysql_error () );
        }
        $array = mysql_fetch_assoc ( $entry );
        if ( !$array ) {
            die ( "Entry does not exist!" );
        }
        return $array;
    }
    function database_entries ()
    {
        $entries = mysql_query ( "SELECT * FROM Entries" );
        if ( !$entries ) {
            die ( "Unable to get number of entries: " . mysql_error () );
        }
        return mysql_num_rows ( $entries );
    }

?>